#include "display.h"


void logic_send_light_msg(spi_send_param *spi_param)
{
  xQueueSend(spi_queue_handle,&spi_param,0);
}



void disp_clear_light(u8 port,u16 length)
{
    spi_send_param *spi_param = NULL;
    u8 *uart_rx_buffer = NULL;
    spi_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
    if (spi_param == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
        return;
    }
    uart_rx_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*(length*3));

    if(uart_rx_buffer == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
        return;
    }

    bzero(uart_rx_buffer,length*3);
    spi_param->send_len = length*3; //待发送数据长度
    spi_param->send_port = port; //待发送数据指定的灯带
    spi_param->send_data = uart_rx_buffer; //待发送的数据具体内容
    logic_send_light_msg(spi_param);
    uart_rx_buffer =NULL; //防止野指针
    spi_param = NULL;


}


void disp_clear_light_front()
{
  disp_clear_light(FRONT_PORT,FRONT_LIGHT); //清空前灯带灯效函数
}

void disp_clear_light_around()
{
  disp_clear_light(RIGHT_PORT,AROUND_LIGHT); //清空右灯带灯效函数
  disp_clear_light(LEFT_PORT,AROUND_LIGHT); //清空左灯带灯效函数
}

void disp_clear_light_corner()
{
  disp_clear_light(CORNER_RIGHT_PORT,CORNER_LIGHT); //清空右三角灯灯效函数
  disp_clear_light(CORNER_LEFT_PORT,CORNER_LIGHT); //清空左三角灯灯效函数
}

void light_white_ws2812(u8 port,u16 length)
{
    spi_send_param *spi_param = NULL;
    u8 *uart_rx_buffer = NULL;
    spi_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
    if (spi_param == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
        return;
    }
    uart_rx_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*(length*3));

    if(uart_rx_buffer == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
        return;
    }

    bzero(uart_rx_buffer,length*3);
    spi_param->send_len = length*3; //待发送数据长度
    spi_param->send_port = port; //待发送数据指定的灯带

    for (int j = 0; j <= length; j += 1)  //算出每个灯珠的颜色
    { 
        uart_rx_buffer[j * 3 + 0] = 255; //G
        uart_rx_buffer[j * 3 + 1] = 255; //R
        uart_rx_buffer[j * 3 + 2] = 255; //B
    }

    spi_param->send_data = uart_rx_buffer; //待发送的数据具体内容
    logic_send_light_msg(spi_param);
    uart_rx_buffer =NULL; //防止野指针
    spi_param = NULL;
}







void light_yellow_ws2812(u8 port,u16 length)
{
    spi_send_param *spi_param = NULL;
    u8 *uart_rx_buffer = NULL;
    spi_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
    if (spi_param == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
        return;
    }
    uart_rx_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*(length*3));

    if(uart_rx_buffer == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
        return;
    }

    bzero(uart_rx_buffer,length*3);
    spi_param->send_len = length*3; //待发送数据长度
    spi_param->send_port = port; //待发送数据指定的灯带

    for (int j = 0; j <= length; j += 1)  //算出每个灯珠的颜色
    { 
        uart_rx_buffer[j * 3 + 0] = 255; //G
        uart_rx_buffer[j * 3 + 1] = 255; //R
        uart_rx_buffer[j * 3 + 2] = 0; //B
    }

    spi_param->send_data = uart_rx_buffer; //待发送的数据具体内容
    logic_send_light_msg(spi_param);
    uart_rx_buffer =NULL; //防止野指针
    spi_param = NULL;
}

void light_green_ws2812(u8 port,u16 length)
{
    spi_send_param *spi_param = NULL;
    u8 *uart_rx_buffer = NULL;
    spi_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
    if (spi_param == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
        return;
    }
    uart_rx_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*(length*3));

    if(uart_rx_buffer == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
        return;
    }

    bzero(uart_rx_buffer,length*3);
    spi_param->send_len = length*3; //待发送数据长度
    spi_param->send_port = port; //待发送数据指定的灯带

    for (int j = 0; j <= length; j += 1)  //算出每个灯珠的颜色
    { 
        uart_rx_buffer[j * 3 + 0] = 255; //G
        uart_rx_buffer[j * 3 + 1] = 0; //R
        uart_rx_buffer[j * 3 + 2] = 0; //B
    }

    spi_param->send_data = uart_rx_buffer; //待发送的数据具体内容
    logic_send_light_msg(spi_param);
    uart_rx_buffer =NULL; //防止野指针
    spi_param = NULL;
}

void light_red_ws2812(u8 port,u16 length)
{
    spi_send_param *spi_param = NULL;
    u8 *uart_rx_buffer = NULL;
    spi_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
    if (spi_param == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
        return;
    }
    uart_rx_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*(length*3));

    if(uart_rx_buffer == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
        return;
    }

    bzero(uart_rx_buffer,length*3);
    spi_param->send_len = length*3; //待发送数据长度
    spi_param->send_port = port; //待发送数据指定的灯带

    for (int j = 0; j <= length; j += 1)  //算出每个灯珠的颜色
    { 
        uart_rx_buffer[j * 3 + 0] = 0; //G
        uart_rx_buffer[j * 3 + 1] = 255; //R
        uart_rx_buffer[j * 3 + 2] = 0; //B
    }

    spi_param->send_data = uart_rx_buffer; //待发送的数据具体内容
    logic_send_light_msg(spi_param);
    uart_rx_buffer =NULL; //防止野指针
    spi_param = NULL;
}

void light_blue_ws2812(u8 port,u16 length)
{
    spi_send_param *spi_param = NULL;
    u8 *uart_rx_buffer = NULL;
    spi_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
    if (spi_param == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
        return;
    }
    uart_rx_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*(length*3));

    if(uart_rx_buffer == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
        return;
    }

    bzero(uart_rx_buffer,length*3);
    spi_param->send_len = length*3; //待发送数据长度
    spi_param->send_port = port; //待发送数据指定的灯带

    for (int j = 0; j <= length; j += 1)  //算出每个灯珠的颜色
    { 
        uart_rx_buffer[j * 3 + 0] = 0; //G
        uart_rx_buffer[j * 3 + 1] = 0; //R
        uart_rx_buffer[j * 3 + 2] = 255; //B
    }

    spi_param->send_data = uart_rx_buffer; //待发送的数据具体内容
    logic_send_light_msg(spi_param);
    uart_rx_buffer =NULL; //防止野指针
    spi_param = NULL;
}


void light1_function_ws2812(u8 port,u16 length)
{
    static u8 i =0;
    static u16 count = 0;

    spi_send_param *spi_param = NULL;
    u8 *uart_rx_buffer = NULL;
    spi_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
    if (spi_param == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
        return;
    }
    uart_rx_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*(length*3));

    if(uart_rx_buffer == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
        return;
    }

    bzero(uart_rx_buffer,length*3);
    spi_param->send_len = length*3; //待发送数据长度
    spi_param->send_port = port; //待发送数据指定的灯带
        for (int j = 0; j <= i; j += 1)  //算出每个灯珠的颜色
        { 
            uart_rx_buffer[j * 3 + 0] = 0; //G
            uart_rx_buffer[j * 3 + 1] = j; //R
            uart_rx_buffer[j * 3 + 2] = 0; //B
        }
        i++;    
        if(i>length)
        {
            i=0;
        }

    spi_param->send_data = uart_rx_buffer; //待发送的数据具体内容
    logic_send_light_msg(spi_param);
    uart_rx_buffer =NULL; //防止野指针
    spi_param = NULL;
}



/*******************************************************************
 * @brief    设置ws281x颜色32位
 * @Author   
 * @DateTime 2024-04-01
 * @param    
 * @return   
 *******************************************************************/
uint32_t ws281x_color(uint8_t red, uint8_t green, uint8_t blue)
{
	return green << 16 | red << 8 | blue;
}


/*******************************************************************
 * @brief    
 * @Author   
 * @DateTime 2024-04-01
 * @param    
 * @return   
 *******************************************************************/
uint32_t ws281x_wheel(uint8_t wheelPos) 
{
	wheelPos = 255 - wheelPos;
	if(wheelPos < 85) 
	{
		return ws281x_color(255 - wheelPos * 3, 0, wheelPos * 3);
	}
	if(wheelPos < 170)
	{
		wheelPos -= 85;
		return ws281x_color(0, wheelPos * 3, 255 - wheelPos * 3);
	}
	wheelPos -= 170;
	return ws281x_color(wheelPos * 3, 255 - wheelPos * 3, 0);
}


/*******************************************************************
 * @brief    填充ws281x颜色
 * @Author   
 * @DateTime 2024-04-01
 * @param    
 * @return   
 *******************************************************************/
void ws281x_set_pixel_color(uint16_t n ,uint32_t GRBcolor,u16 length,u8 *uart_rx_buffer)
{
	uint8_t i;
	if(n < length)
	{   
       uart_rx_buffer[n] = (u8)((GRBcolor>>16)&0xFF);
       uart_rx_buffer[n+1] =(u8)((GRBcolor>>8)&0xFF);
       uart_rx_buffer[n+2] =(u8)(GRBcolor&0xFF);
	}
}